#include <REGX52.H>
#include "Delay.h"
#include "Key.h"
#include "Nixie.h"
#include "Timer0.h"
#include "oled.h"
/*
对于180°舵机
t = 0.5ms——————-舵机会转动 0 ° 
t = 1.0ms——————-舵机会转动 45°
t = 1.5ms——————-舵机会转动 90°
t = 2.0ms——————-舵机会转动 135°
t = 2.5ms——————-舵机会转动180
*/

sbit Control_Actuator=P2^7;//舵机控制口
sbit Motor=P1^0;
char Compare=0;
unsigned char Motor_Counter,Motor_Compare=0;
unsigned char Counter;	//计数值和比较值，用于输出PWM
unsigned char KeyNum=0,Speed;
int count=0;

void main()
{
	Timer0_Init();//time0初始化
	
	while(1)
	{
		KeyNum=key_scan(0);
		if(KeyNum==2)
		{
			Speed++;
			Speed%=4;
			if(Speed==0){Motor_Compare=0;}	//设置比较值，改变PWM占空比
			if(Speed==1){Motor_Compare=40;}
			if(Speed==2){Motor_Compare=70;}
			if(Speed==3){Motor_Compare=100;}
		}
		Nixie(1,Speed);
	}
}

void Timer0_Routine() interrupt 1 //0.5ms一个中断
	
{
	TL0 = 0x33;		//设置定时初始值
	TH0 = 0xFE;		//设置定时初始值
	Counter++;
	Counter%=40;	//计数值变化范围限制在0~40 20ms
	Motor_Counter++;
	Motor_Counter%=100;	//计数值变化范围限制在0~99
	count++;
	if(count>=500)
	{
		count=0;
		Compare++;	
	}
	if(Compare>5)
		Compare=0;
	if(Counter<=Compare)	//计数值小于比较值   舵机转动
	{
		Control_Actuator=1;		//输出1
	}
	else if(Counter>Compare)				//计数值大于比较值
	{
		Control_Actuator=0;		//输出0
	}
//**********************************************************
	if(Motor_Counter<Motor_Compare)	//计数值小于比较值  风扇调速
	{
		Motor=1;		//输出1
	}
	else	if(Motor_Counter>=Motor_Compare)			//计数值大于比较值
	{
		Motor=0;		//输出0
	}
}
